<template>
  <div class="single">
    <el-row>
      <el-col :span="24">
        <el-form
          :model="formModel"
          :rules="rules"
          ref="formModel"
          label-width="110px"
          class="demo-formModel"
        >

          <!-- 年度 -->
          <el-row>
            <el-col :span="8">
              <el-form-item label="年度" prop="nd">
                <el-input v-model="formModel.nd" placeholder="请输入年度" class="el_select"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="学期" prop="xq">
                <el-input v-model="formModel.xq" placeholder="请输入年度学期" class="el_select"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="课程名称" prop="kcmc">
                <el-input v-model="formModel.kcmc" placeholder="请输入课程名称" class="el_select"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 所学专业 -->
          <el-row>
            <el-col :span="8">
              <el-form-item label="所属教学团队" prop="ssjxtd">
                <el-input v-model="formModel.ssjxtd" placeholder="请输入所属教学团队" class="el_select"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="16">
              <el-form-item label="教学团队类型" prop="jxtdlx">
                <el-input v-model="formModel.jxtdlx" placeholder="请输入教学团队类型（总部/分部教学核心团队、总部/分部实施团队）"
                          style="width:89%;"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 学位名称 -->
          <el-row>
            <el-col :span="8">
              <el-form-item label="团队内角色" prop="tdnjs">
                <el-input v-model="formModel.tdnjs" placeholder="请输入团队内角色" class="el_select"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="所属团队服务区域" prop="fwqy">
                <el-input v-model="formModel.fwqy" placeholder="请输入所属团队服务区域" class="el_select"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="所属团队服务学生数量" prop="fwxssl">
                <el-input v-model="formModel.fwxssl" placeholder="请输入所属团队服务学生数量" class="el_select"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 学位授予年月 -->
          <el-row>
            <el-col :span="8">
              <el-form-item label="所属团队负责人" prop="tdfzr">
                <el-input v-model="formModel.tdfzr" placeholder="请输入所属团队负责人" class="el_select"></el-input>
              </el-form-item>
            </el-col>

          </el-row>

          <el-row class="btns">
            <el-col :span="24">
              <el-form-item>
                <el-button type="primary" @click="submitForm('formModel')"
                >{{ btnname }}
                </el-button
                >
                <el-button @click="resetForm('formModel')">重置</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
  name: 'studyexp',
  data () {
    return {
      formModel: {
        nd: '',
        xq: '',
        kcmc: '',
        ssjxtd: '',
        jxtdlx: '',
        tdnjs: '',
        fwqy: '',
        fwxssl: '',
        tdfzr: '',
      },
      rules: {
        nd: [
          {required: true, message: '请输入年度', trigger: 'blur'},
        ],
        xq: [
          {required: true, message: '请输入年度学期', trigger: 'blur'},
        ],
        kcmc: [
          {required: true, message: '请输入课程名称', trigger: 'blur'},
        ],
        ssjxtd: [
          {required: true, message: '请输入所属教学团队', trigger: 'blur'},
        ],
        jxtdlx: [
          {required: true, message: '请输入教学团队类型', trigger: 'blur'},
        ],
        tdnjs: [
          {required: true, message: '请输入团队内角色', trigger: 'blur'},
        ],
        fwqy: [
          {required: true, message: '请输入所属团队服务区域', trigger: 'blur'},
        ],
        fwxssl: [
          {required: true, message: '请输入所属团队服务学生数量', trigger: 'blur'},
        ],
        tdfzr: [
          {required: true, message: '请输入所属团队负责人', trigger: 'blur'},
        ],
      },
      btnname: '添加',
      page: {
        id: '',
        op: 'add'
      },
    }
  },
  mounted () {
    this.initdata()
  },
  methods: {
    initdata () {
      var params = this.$route.params
      console.log(params)
      this.page.id = params.id
      this.page.op = params.op
      this.get_detail()
    },
    get_detail () {
      var params = {jsId: this.page.id}
      this.$http({
        url: this.$http.adornUrl('/rxjxtdxx/getJsAllRxjxtdxx'),
        method: 'get',
        params: this.$http.adornParams(params)
      }).then(({data}) => {
        console.log(data.data)
        if (data && data.code === 0) {
          if (data.data.length > 0) {
            this.page.op = 'detail'
            this.btnname = '修改'
            this.formModel = data.data[0]
          } else {
            this.page.op = 'add'
            this.btnname = '添加'
          }
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    submitForm (formName) {
      this.formModel.jsId = this.$store.state.user.jsid
      let formData = this.get_formdata(this.formModel)
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = ''
          if (this.page.op == 'add') {
            url = '/rxjxtdxx/save'
          } else {
            url = '/rxjxtdxx/update'
          }
          this.$http({
            url: this.$http.adornUrl(url),
            data: formData,
            method: 'post'
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.$nextTick(() => {
                    if (this.page.op == 'add') {
                      this.$refs[formName].resetFields()
                    }
                  })
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    },
    get_formdata (config) {
      console.log(config)
      //对象转formdata格式
      const formData = new FormData()
      //循环传入的值转换formData
      Object.keys(config).forEach((key) => {
        formData.append(key, config[key])
      })
      return formData
    }
  },
}
</script>


<style>
.single {
  padding: 0.5rem;
}

.el_select {
  width: 15rem;
}

.btns {
  padding-left: 22rem;
}
</style>

